﻿/* 1. Write a program that fills and prints a matrix of size (n, n) as shown below: (examples for n = 4) 
 
        a)  1  5  9 13       b)  1  8  9 16       c)  7 11 14 16       d)***   1 12 11 10
            2  6 10 14           2  7 10 15           4  8 12 15               2 13 16  9
            3  7 11 15           3  6 11 14           2  5  9 13               3 14 15  8
            4  8 12 16           4  5 12 13           1  3  6 10               4  5  6  7    */

using System;

public class SeveralMatrices
{
    public static int n;
    public static int[,] matrix;
    public static int element;
    public static int digitsOfN;
    public static int elementsOfMatrixRow;

    public static int r = 1;
    public static int c = 0;

    public static void Main()
    {
        DataInput();
        matrix = new int[n, n];
        ExampleA();
        ExampleB();
        ExampleC();
        ExampleD();
    }

    public static void DataInput()
    {
        do
        {
            Console.Clear();
            Console.Write("\nn = ");
            if (int.TryParse(Console.ReadLine(), out n) && (0 < n))
                break;
        } while (true);

        Console.WriteLine();

        digitsOfN = Digits(n);
        elementsOfMatrixRow = 2 + n * (1 + digitsOfN);
    }

    public static int Digits(int x)
    {
        int digits = 0;

        if (x == 0)
            return 1;

        while (x > 0)
        {
            x /= 10;
            digits++;
        }

        return digits;
    }

    public static void ExampleA()
    {
        element = 1;

        for (int col = 0; col < n; col++)
            for (int row = 0; row < n; row++)
                matrix[row, col] = element++;

        Print(0);
    }

    public static void ExampleB()
    {
        element = 1;

        for (int col = 0; col < n; col++)
            for (int row = 0; row < n; row++)
                if (col % 2 == 0)
                    matrix[row, col] = element++;
                else
                    matrix[n - row - 1, col] = element++;

        Print(0);
    }

    public static void ExampleC()
    {
        element = 1;

        for (int diagonal = 0; diagonal < 2 * n - 1; diagonal++)
            for (int col = 0; col <= diagonal; col++)
                for (int row = 0; row <= diagonal; row++)
                    if ((row < n) && (col < n) && (row + col == diagonal))
                        matrix[n - row - 1, col] = element++;

        Print(0);
    }

    public static void ExampleD()
    {
        int lastElement = n * n;

        int row = 0;
        int col = 0;

        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++)
                matrix[i, j] = 0;

        matrix[0, 0] = 1;
        element = 2;

        while (element <= lastElement)
        {
            if ((0 <= row + r) && (row + r < n) && (0 <= col + c) && (col + c < n) && (matrix[row + r, col + c] == 0))
                matrix[row += r, col += c] = element++;
            else
                ChangeDirection();
        }

        Print(0);
    }

    public static void ChangeDirection()
    {
        if (r == 1)
        {
            r = 0;
            c = 1;
        }
        else if (r == -1)
        {
            r = 0;
            c = -1;
        }
        else if (c == 1)
        {
            r = -1;
            c = 0;
        }
        else
        {
            r = 1;
            c = 0;
        }
    }

    public static void Print(int x)
    {
        for (int i = 0; i < n; i++)
        {
            Console.Write("{0}{1}{2}", new string(' ', (1 + x)), 
                                            new string(' ', (1 + digitsOfN - Digits(matrix[i, 0]))), matrix[i, 0]);
            for (int j = 1; j < n; j++)
                Console.Write("{0}{1}", new string(' ', (2 + digitsOfN - Digits(matrix[i, j]))), matrix[i, j]);

            Console.WriteLine();
        }

        Console.WriteLine();
    }
}